package com.example.kcb;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.util.Calendar;

public class CourseStatActivity extends AppCompatActivity {
    private TextView tvStart, tvEnd, tvReal, tvShould;
    private DatabaseHelper helper;
    private String startDate, endDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_stat);

        helper = new DatabaseHelper(this);
        tvStart = (TextView) findViewById(R.id.tvStart);
        tvEnd   = (TextView) findViewById(R.id.tvEnd);
        tvReal  = (TextView) findViewById(R.id.tvReal);
//        tvShould= (TextView) findViewById(R.id.tvShould);

        // 1. 接收日期
        Intent i = getIntent();
        startDate = i.getStringExtra("start");
        endDate   = i.getStringExtra("end");
        tvStart.setText(startDate);
        tvEnd.setText(endDate);

        // 2. 总计课时
        int real = queryRealHours();
        tvReal.setText("总计:" + real + "节");

//        // 3. 应计课时
//        int should = queryShouldHours();
//        tvShould.setText("应计:" + should + "节");
    }

    /* 实际已上课时 */
    private int queryRealHours() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor c = db.rawQuery(
                "SELECT COUNT(*) FROM course_records WHERE record_time BETWEEN ? AND ?",
                new String[]{startDate, endDate});
        int cnt = 0;
        if (c.moveToFirst()) cnt = c.getInt(0);
        c.close();
        return cnt;
    }

    /* 应计课时 = Σ(段内周一~周日天数 × 对应星期课程数) */
    private int queryShouldHours() {
        SQLiteDatabase db = helper.getReadableDatabase();

        // 1. 先查出 courses 表里 周一~周日 各有多少节课
        int[] weekCourses = new int[8];   // 0占位 1=周一 … 7=周日
        Cursor c = db.rawQuery(
                "SELECT day, COUNT(*) FROM courses GROUP BY day", null);
        while (c.moveToNext()) {
            int day = c.getInt(0);        // 1~7
            if (day >= 1 && day <= 7) weekCourses[day] = c.getInt(1);
        }
        c.close();

        // 2. 计算日期段内 周一~周日 各有多少天
        int[] weekDays = countWeekDays(startDate, endDate);

        // 3. 相乘累加
        int total = 0;
        for (int day = 1; day <= 7; day++) {
            total += weekDays[day] * weekCourses[day];
        }
        return total;
    }

    /* 返回数组[1]~[7] 分别代表周一~周日天数 */
    private int[] countWeekDays(String start, String end) {
        int[] cnt = new int[8];
        try {
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(start));
            Calendar endCal = Calendar.getInstance();
            endCal.setTime(sdf.parse(end));

            while (!cal.after(endCal)) {
                int week = cal.get(Calendar.DAY_OF_WEEK); // 1=周日 … 7=周六
                int dayIndex = week == Calendar.SUNDAY ? 7 : week - 1; // 转 1=周一 … 7=周日
                cnt[dayIndex]++;
                cal.add(Calendar.DATE, 1);
            }
        } catch (Exception ignore) {}
        return cnt;
    }
}
